package com.kukudexin;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 酷酷的鑫
 * @Date: 2021/8/6 11:07
 * @description:
 */
public class Test08 {
    public static void main(String[] args) {
        //定义存储Circle圆形的集合
        List<Circle> list = new ArrayList<>();
        list.add(new Circle());
        List<Rectangle> rectangles = new ArrayList<>();
        rectangles.add(new Rectangle());
        //创建画布
        Canvas canvas = Canvas.getInstance();
        canvas.drawAll3(list);
        canvas.drawAll3(rectangles);
    }

    //定义Shape图形抽象类
    static abstract class Shape {
        //图形有一个在画布上绘画的功能, 通过参数来接收一个画布
        public abstract void draw(Canvas c);
    }

    //定义圆形类继承Shape
    static class Circle extends Shape {
        //重写抽象方法,实现绘画功能
        @Override
        public void draw(Canvas c) {
            System.out.println("在画布" + c + "上画了一个圆");
        }
    }
    //定义矩形类继承Shape

    static class Rectangle extends Shape {
        //重写抽象方法,实现绘画功能
        @Override
        public void draw(Canvas c) {
            System.out.println("在画布" + c + "上画了一个矩形");
        }
    }


}

//定义画布类
class Canvas {
//        private static Canvas lazy = null;

    //        public static synchronized Canvas getInstance() {
//            if (lazy == null) {
//                lazy = new Canvas();
//            }
//            return lazy;
//        }
    private static Canvas lazy = new Canvas();
    public static Canvas getInstance(){
        return lazy;
    }

    private Canvas() {
    }

    //方法参数实际需要一种泛型, 可以表示所有Shape泛型List的子类. 可以使用有界通配 符
    public void drawAll3(List<? extends Test08.Shape> list) {
        for (Test08.Shape shape : list) {
            shape.draw(lazy);
        }
    }
}
